﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Core.Interfaces;
using Core.Interfaces.IDAO;
using Core.Interfaces.IPresenters;
using Core.Unity;
using Microsoft.Practices.Unity;
namespace Core.Presenters
{
    class ActionPresenter
    {
        private IBuildingDAO _buildingDAO;
        private IActionDAO _actionDAO;
        private IActionView _actionView;
        private IAction _action;
        private List<IBuilding> _buildingsOnAction = new List<IBuilding>();
        private List<IBuilding> _buildings;
        private IOwnedBuildingDAO _ownedBuildingDAO;
        private Dictionary<IBuilding, int> _buildingPrices = new Dictionary<IBuilding, int>();

        public ActionPresenter(IActionView actionView , IAction action = null)
        {
            _buildingDAO = ResolverContainer.Resolver.Resolve<IBuildingDAO>();
            _actionDAO = ResolverContainer.Resolver.Resolve<IActionDAO>();
            _ownedBuildingDAO = ResolverContainer.Resolver.Resolve<IOwnedBuildingDAO>();
            _actionView = actionView;
            _buildings = _buildingDAO.GetAll(false);
            var owned_buildings = _ownedBuildingDAO.GetAll(true);
            var ob = (from building in _buildings
                      let b = owned_buildings.Any(x => x.Building.RegisterID == building.RegisterID)
                      where b
                      select building).ToList();

            foreach (IBuilding building in ob)
            {
                _buildings.Remove(building);
            }
            if (action == null)
            {
                _action = ResolverContainer.Resolver.Resolve<IAction>();
            }
            else
            {
                _action = action;
                _buildingsOnAction = _buildingDAO.GetAllBuildingsByAction(action);
                _action.Buildings = _buildingsOnAction;
                
               
                _actionDAO.GetPrices(_action);
            }
            _actionView.Buildings.DataSource = _buildings;
            _actionView.BuildingsOnAction.DataSource = _buildingsOnAction;
            _actionView.Action.DataSource = _action;
            _actionView.SaveAction += SaveActionEvent;
            _actionView.AddAllToLeft += AddAllToLeft;
            _actionView.AddAllToRight += AddAllToRight;
            _actionView.AddToLeft += AddToLeft;
            _actionView.AddToRight += AddToRight;
            _actionView.ShowDialog();

        }

        private void SaveActionEvent(object sender, EventArgs eventArgs)
        {
            _action.Buildings = _buildingsOnAction;
            if (_action.FromDatabase())
            {
                _actionDAO.DeleteAllLinks(_action);
            }
            if (_actionDAO.SaveOrUpdateAndCommit(_action))
            {
                _actionDAO.SavePrice(_action);
                _actionView.Close();
            }
        }

        private void AddToRight(object sender, EventArgs eventArgs)
        {
            var b =(IBuilding) _actionView.Buildings.Current;
            if (b != null)
            {
                _buildingPrices.Add(b,0);
                _buildings.Remove(b);
                _buildingsOnAction.Add(b);
                ResetBindings();
            }
        }

        private void AddToLeft(object sender, EventArgs eventArgs)
        {
            var b = (IBuilding)_actionView.BuildingsOnAction.Current;
            if (b != null)
            {
                _buildingPrices.Remove(b);
                _buildingsOnAction.Remove(b);
                _buildings.Add(b);
                ResetBindings();
            }
        }

        private void AddAllToLeft(object sender, EventArgs eventArgs)
        {
            
        }

        private void AddAllToRight(object sender,EventArgs eventArgs)
        {
            
        }

        private void ResetBindings()
        {
            _actionView.Buildings.ResetBindings(false);
            _actionView.BuildingsOnAction.ResetBindings(false);
        }
    }
}
